/**
 * 华丽列表页(发现页/讲座页) 通用的脚本库
 */

/* 列表核心的处理 */
var nowScrollTop = 0;

function MakeMiniRefreshWithSwiper(swiperQuery, listQuery) {
	return new MiniRefresh({
		container: '#Minirefresh',
		// isUseBodyScroll: true,
		isScrollBar: false,
		down: {
			isAuto: false,
			isAllowAutoLoading: false,
			callback: function() {
				isEnd = false;
				RefreshUserInfo(() => {
					refreshPage(listType);
				});
			},
		},
		up: {
			onScroll: function(scrollTop) {
				if (scrollTop > 10 && Math.abs(scrollTop - nowScrollTop) <= 10) {
					return;
				}
				if (scrollTop >= screen.width * 0.05) {
					$('header').addClass('shadowed');
				} else {
					$('header').removeClass('shadowed');
				}
				/*   以下是轮播和列表的视差用动画效果   */
				/*if (scrollTop > 0 && scrollTop <= screen.width * 0.52) {
					// 0->1  0.5->0
					$(swiperQuery)[0].style.opacity = 1 - (scrollTop / screen.width);
					$(listQuery)[0].style.marginTop = Math.round(-scrollTop / 2 - 0.04 * screen.width) + 'px';
				} else if (scrollTop > screen.width * 0.52) {
					$(swiperQuery)[0].style.opacity = '0.5';
					$(listQuery)[0].style.marginTop = -0.30 * screen.width + 'px';
				} else {
					$(swiperQuery)[0].style.opacity = '1';
					$(listQuery)[0].style.marginTop = -0.04 * screen.width + 'px';
				}*/
			},
			callback: function() {
				if (isEnd) {
					miniRefresh.endUpLoading(true);
					return;
				}
				RefreshUserInfo(() => {
					drawPage();
					showList(lastId);
				});
			}
		}
	});
}

/* 院校位置选择的处理 */
function InitEduSelect() {
	let eduId = plus.storage.getItem('viewEduId');
	if (eduId == null) {
		try {
			let userInfo = JSON.parse(plus.storage.getItem('userInfo'));
			eduId = Pad(parseInt(userInfo['uEduId']), 12);
		} catch (e) {
			eduId = '000000000000';
		}
	}
	return eduId;
}



//    上面是公共库性质的函数(页面只调用), 使用大驼峰
// ------------------------------------------------------
//    下面是内联函数(就像直接写在页面里), 使用小驼峰



/* getEduId是SelectEdu的回调 */
function getEduId(eduIdNew) {
	eduId = eduIdNew;
	plus.storage.setItem('viewEduId', eduIdNew);
	refreshPage(listType);
}

/* refreshPage */
function refreshPage(type) {
	if (type == null) return; // 屏蔽通用的refreshPage事件
	eduId = InitEduSelect();
	drawPage();
	RefreshUserInfo(function(phoneno) {
		drawPage();
	});
	changeListType(type);
}

/* changeListType */
function changeListType(type) {
	listType = type;
	isEnd = false;
	miniRefresh.scrollTo(0);
	showList(0);
}

/* drawPage 填充header里的信息(头像和学校名) */
function drawPage() {
	let userInfo = JSON.parse(plus.storage.getItem('userInfo'));
	if (userInfo === null) return false;
	// -----------头像
	$('.circle').attr("src", SERVER_LOC + userInfo["uHeadImgLoc"]);
	// -----------学校名
	function drawSchoolName(schoolName) {
		$('#CampusName').text(schoolName);
		let schoolLen = schoolName.length;
		let campusNameDOM = document.querySelector("#CampusName");
		if (schoolLen >= 5) {
			if (schoolLen > 6)
				campusNameDOM = campusNameDOM.substring(0, 6);
			campusNameDOM.style.right = '1.2vw';
			campusNameDOM.style.fontSize = '3.2vw';
		}
	}
	CallAJAX("getEduDetail", {
		'eduId': eduId,
	}, function(data) {
		if (data["res"] == "ok") {
			drawSchoolName(data["eduDetail"][0]);
		} else {
			drawSchoolName(userInfo['uEduSchoolName']);
		}
	}, function() {
		console.log('?');
		drawSchoolName(userInfo['uEduSchoolName']);
	});
}

/* 切割tag */
function getTags(tagsText) {
	return tagsText.split('|');
}
